import { defineStore } from 'pinia';

// 定义井字棋游戏状态store
export const useTicTacToeStore = defineStore('tictactoe', {
  state: () => ({
    // 游戏棋盘状态
    board: Array(3).fill().map(() => Array(3).fill(null)),
    
    // 游戏玩家状态
    players: [
      { id: 1, name: '玩家1', piece: 'X', wins: 0 },
      { id: 2, name: '玩家2', piece: 'O', wins: 0 }
    ],
    
    // 当前游戏状态
    currentPlayerId: 1,
    winner: null,
    gameOver: false,
    tie: false,
    
    // 游戏设置
    settings: {
      showAnimations: true
    },
    
    // UI状态
    showSettings: false
  }),

  getters: {
    /**
     * 获取当前玩家
     * @returns {Object} 当前玩家对象
     */
    currentPlayer(state) {
      return state.players.find(player => player.id === state.currentPlayerId);
    },
    
    /**
     * 检查是否有玩家获胜
     * @returns {Boolean} 是否有玩家获胜
     */
    hasWinner(state) {
      return state.winner !== null;
    },
    
    /**
     * 获取获胜玩家
     * @returns {Object|null} 获胜玩家对象或null
     */
    getWinner(state) {
      if (!state.winner) return null;
      return state.players.find(player => player.id === state.winner);
    },
    
    /**
     * 获取当前玩家的棋子类型
     * @returns {String} 棋子类型 ('X' 或 'O')
     */
    currentPlayerPiece(state) {
      const player = state.players.find(player => player.id === state.currentPlayerId);
      return player ? player.piece : null;
    }
  },

  actions: {
    /**
     * 初始化游戏
     */
    initGame() {
      this.board = Array(3).fill().map(() => Array(3).fill(null));
      this.currentPlayerId = 1;
      this.winner = null;
      this.gameOver = false;
      this.tie = false;
    },
    
    /**
     * 落子
     * @param {Number} row - 行索引
     * @param {Number} col - 列索引
     * @returns {Boolean} 是否落子成功
     */
    makeMove(row, col) {
      // 检查游戏是否已结束或位置是否已被占用
      if (this.gameOver || this.board[row][col] !== null) {
        return false;
      }
      
      // 获取当前玩家的棋子类型
      const piece = this.currentPlayerPiece;
      if (!piece) return false;
      
      // 落子
      this.board[row][col] = piece;
      
      // 检查是否获胜
      if (this.checkWin(row, col, piece)) {
        this.winner = this.currentPlayerId;
        this.gameOver = true;
        this.players = this.players.map(player => 
          player.id === this.currentPlayerId ? { ...player, wins: player.wins + 1 } : player
        );
        return true;
      }
      
      // 检查是否平局
      if (this.checkTie()) {
        this.tie = true;
        this.gameOver = true;
        return true;
      }
      
      // 切换玩家
      this.currentPlayerId = this.currentPlayerId === 1 ? 2 : 1;
      return true;
    },
    
    /**
     * 检查是否获胜
     * @param {Number} i - 行索引
     * @param {Number} j - 列索引
     * @param {String} pieceType - 棋子类型
     * @returns {Boolean} 是否获胜
     */
    checkWin(i, j, pieceType) {
      // 检查行
      let rowWin = true;
      for (let k = 0; k < 3; k++) {
        if (this.board[i][k] !== pieceType) {
          rowWin = false;
          break;
        }
      }
      if (rowWin) return true;

      // 检查列
      let colWin = true;
      for (let k = 0; k < 3; k++) {
        if (this.board[k][j] !== pieceType) {
          colWin = false;
          break;
        }
      }
      if (colWin) return true;

      // 检查对角线 (从左上到右下)
      if (i === j) {
        let diagWin = true;
        for (let k = 0; k < 3; k++) {
          if (this.board[k][k] !== pieceType) {
            diagWin = false;
            break;
          }
        }
        if (diagWin) return true;
      }

      // 检查对角线 (从右上到左下)
      if (i + j === 2) {
        let diagWin = true;
        for (let k = 0; k < 3; k++) {
          if (this.board[k][2 - k] !== pieceType) {
            diagWin = false;
            break;
          }
        }
        if (diagWin) return true;
      }

      return false;
    },
    
    /**
     * 检查是否平局
     * @returns {Boolean} 是否平局
     */
    checkTie() {
      for (let i = 0; i < 3; i++) {
        for (let j = 0; j < 3; j++) {
          if (this.board[i][j] === null) {
            return false; // 还有空位，不是平局
          }
        }
      }
      return true; // 棋盘已满，平局
    },
    
    /**
     * 重置游戏
     */
    resetGame() {
      this.initGame();
    },
    
    /**
     * 切换设置面板显示状态
     */
    toggleSettings() {
      this.showSettings = !this.showSettings;
    }
  }
});